<template>
  <el-dialog title="系数配置" :close-on-click-modal="false" @close="end" :visible.sync="visible" width="800px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="150px">
      <el-form-item label="部门名称" prop="bmMc">
        <el-input v-model="dataForm.bmMc" :disabled="true" placeholder="部门名称" style="width: 90%;"></el-input>
      </el-form-item>
      <el-form-item label="部门系数" prop="config01">
        <el-input-number size="medium" v-model="dataForm.config01" style="width: 90%;"></el-input-number>
      </el-form-item>
      <el-form-item label="部门考核基础分" prop="config02">
        <el-input-number size="medium" v-model="dataForm.config02" style="width: 90%;"></el-input-number>
      </el-form-item>
      <el-form-item label="部门考核分级" prop="config03">
        <el-select :clearable="true" v-model="dataForm.config03" placeholder="请选择部门考核分级" class="xiaLa" style="width: 90%;">
          <el-option
            v-for="(item,index) in  options"
            :key="index" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="end">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import {
    saveOrUpdatByDeptCode,
    getByDeptCode
  } from '@/api/jiangjin/jiangjinjggxxspz'

  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          bmId: '',
          bmCode: '',
          bmMc: '',
          config01: '',
          config02: '',
          config03: '',
        },
        options:[
            {label:'A',value:'A'},
            {label:'B',value:'B'},
            {label:'C',value:'C'},
            {label:'D',value:'D'},
            // {label:'A1',value:'A1'},
            // {label:'B1',value:'B1'},
            // {label:'C1',value:'C1'},
            // {label:'D1',value:'D1'},
            // {label:'A2',value:'A2'},
            // {label:'B2',value:'B2'},
            // {label:'C2',value:'C2'},
            // {label:'D2',value:'D2'},
            ],
        dataRule: {

          bmCode: [{
            required: true,
            message: '部门编码不能为空',
            trigger: 'blur'
          }],
          bmMc: [{
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          }],
          config01: [{
            required: true,
            message: '部门系数不能为空',
            trigger: 'blur'
          }],
          
          // config02: [{
          //   required: true,
          //   message: '部门考核基础分不能为空',
          //   trigger: 'blur'
          // }],
          // config03: [{
          //   required: true,
          //   message: '部门考核分级 ABCD不能为空',
          //   trigger: 'blur'
          // }]
        }
      }
    },
    methods: {
      init(r) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          if (r.deptCode) {
            getByDeptCode(r.deptCode).then(response => {
              if(response.data.data){
               this.dataForm = response.data.data
              }else{
               this.dataForm.bmId = r.deptId
               this.dataForm.bmCode = r.deptCode
               this.dataForm.bmMc = r.deptName
              }
            })
          }
         })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            saveOrUpdatByDeptCode(this.dataForm).then(data => {
              this.$notify.success('配置成功')
              this.dataForm = {
                  id: 0,
                  bmCode: '',
                  bmMc: '',
                  config01: '',
                  config02: '',
                  config03: '',
                },
                this.visible = false
              this.$emit("refresh");
            }).catch(() => {
              this.canSubmit = true;

            });

          }
        })
      },
      end(){
        this.dataForm={
                  id: 0,
                  bmCode: '',
                  bmMc: '',
                  config01: '',
                  config02: '',
                  config03: '',
                }
         this.visible = false
      }
    }
  }

</script>
